package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: hujian
 * @Date: 2019/8/12 11:01
 * @Description: <描述>
 */
public class FutherTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long start = System.currentTimeMillis();

        // 等凉菜
        Callable cal = () -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("等待5秒");
            Thread.sleep(5000);
            return "凉菜准备完毕";
        };

        FutureTask<String> ft1 = new FutureTask<String>(cal);
        new Thread(ft1).start();
        System.out.println(66666);


        // 等包子 -- 必须要等待返回的结果，所以要调用join方法
        Callable ca2 = () -> {
            try {
                Thread.sleep(1000*10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "包子准备完毕";
        };
        FutureTask<String> ft2 = new FutureTask<String>(ca2);
        new Thread(ft2).start();

        System.out.println(ft1.get()+">>>ft1");
        System.out.println(ft2.get()+">>>ft2");
        long end = System.currentTimeMillis();
        System.out.println("准备完毕时间："+(end-start));
    }
}
